package com.kaikeba.controller;


import com.kaikeba.bean.BootStrapTableCourier;
import com.kaikeba.bean.Courier;
import com.kaikeba.bean.Message;
import com.kaikeba.bean.ResultData;
import com.kaikeba.mvc.ResponseBody;
import com.kaikeba.service.CourierService;
import com.kaikeba.util.JSONUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * @description:
 * @author: ManolinCoder
 * @time: 2021/9/3
 */
public class CourierController {

    @ResponseBody("/courier/list.do")
    public String list(HttpServletRequest request, HttpServletResponse response){
        //1.获取查询数据的起始索引值
        int offset = Integer.parseInt(request.getParameter("offset"));
        //2.获取当前页要查询的数据量
        int pageNumber = Integer.parseInt(request.getParameter("pageNumber"));
        //3.进行分页查询
        List<BootStrapTableCourier> list = CourierService.findAll(true, offset, pageNumber);
        //4.获取数据总条数
        Map<String, Integer> console = CourierService.console();
        Integer total = console.get("data_size");
        //5.将集合封装为bootstrap-table识别的格式
        ResultData<BootStrapTableCourier> data = new ResultData<>();
        data.setRows(list);
        data.setTotal(total);
        String json = JSONUtil.toJSON(data);
        return json;
    }

    @ResponseBody("/courier/insert.do")
    public String insert(HttpServletRequest request, HttpServletResponse response){
        String cname = request.getParameter("cname");
        String cphonenumber = request.getParameter("cphonenumber");
        String cidcardnum = request.getParameter("cidcardnum");
        String cpassword = request.getParameter("cpassword");
        Courier courier = new Courier(cname,cphonenumber,cidcardnum,cpassword);
        boolean flag = CourierService.insert(courier);
        Message msg = new Message();
        if (flag){
            //录入成功
            msg.setStatus(0);
            msg.setResult("快递员信息录入成功!");
        }else {
            //录入失败
            msg.setStatus(-1);
            msg.setResult("快递员信息录入失败!");
        }
        String json = JSONUtil.toJSON(msg);
        return json;
    }

    @ResponseBody("/courier/find.do")
    public String find(HttpServletRequest request,HttpServletResponse response){
        String phoneNumber = request.getParameter("phoneNumber");
        Courier c = CourierService.findByPhoneNumber(phoneNumber);
        Message msg = new Message();
        if (c == null){
            msg.setStatus(-1);
            msg.setResult("手机号不存在");
        }else {
            msg.setStatus(0);
            msg.setResult("查询成功");
            msg.setData(c);
        }
        String json = JSONUtil.toJSON(msg);
        return json;
    }

    @ResponseBody("/courier/update.do")
    public String update(HttpServletRequest request,HttpServletResponse response){
        int id = Integer.parseInt(request.getParameter("id"));
        String cname = request.getParameter("cname");
        String cPhoneNumber = request.getParameter("cPhoneNumber");
        String cIdCardNum = request.getParameter("cIdCardNum");
        String cPassword = request.getParameter("cPassword");
        Courier newc = CourierService.findById(id);
        newc.setCname(cname);
        newc.setcPhoneNumber(cPhoneNumber);
        newc.setcIdCardNum(cIdCardNum);
        newc.setcPassword(cPassword);
        boolean flag = CourierService.update(id, newc);
        Message msg = new Message();
        if (flag){
            msg.setStatus(0);
            msg.setResult("修改成功");
        }else {
            msg.setStatus(-1);
            msg.setResult("修改失败");
        }
        String json = JSONUtil.toJSON(msg);
        return json;
    }

    @ResponseBody("/courier/delete.do")
    public String delete(HttpServletRequest request,HttpServletResponse response){
        int id = Integer.parseInt(request.getParameter("id"));
        boolean flag = CourierService.delete(id);
        Message msg = new Message();
        if (flag){
            msg.setStatus(0);
            msg.setResult("删除成功");
        }else {
            msg.setStatus(-1);
            msg.setResult("删除失败");
        }
        String json = JSONUtil.toJSON(msg);
        return json;
    }
}
